require 'pathname'

class Cell < ActiveRecord::Base
  attr_accessor :cell_file
  
  before_create :save_file_details
  after_save :save_file
  after_destroy :delete_file
    
  def save_file_details
    self.filename = cell_file.original_filename
    self.mime = cell_file.content_type.strip
  end
  
  def save_file
    File.open(path, "wb") { |f| f.write(cell_file.read) }
  end
  
  def delete_file
    File.delete(path)
  end
  
  def path
    return File.join('public/data', filename)
  end
  
  def contents
    return Pathname.new(path).read
  end
end
